package LecoLi.Item03;

import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.demo.R;
import com.github.mikephil.charting.data.Entry;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Handler;

import LecoLi.Util.Item03.LineChartViewPagerAdapter;

/**
 * Created by Le Co on 2017/5/15.
 */

public class LineChartViewPage extends Fragment{
    private View view;
    private TextView textView;
    private ViewPager viewPager;
    private LineChartViewPagerAdapter lineChartViewPagerAdapter;
    private List<Fragment> fragments;
    private LineChartFragment GuanglineChartFragment,KonglineChartFragment,WenlineChartFragment;
    private ArrayList<Entry> Guanglist,KongList,WenList;
    private ArrayList<String> strings;
    private FragmentManager fragmentManager;
    private String [] names = {"光照强度","空气湿度","空气温度"};

    private android.os.Handler handler = new android.os.Handler(){
        @Override
        public void handleMessage(Message msg) {
            int s = 0 ;
            if (s!=msg.arg1){
                s=msg.arg1;
                switch (s){
                    case 0:
                        textView.setText(names[0]);
                        break;
                    case 1:
                        textView.setText(names[1]);
                        break;
                    case 2:
                        textView.setText(names[2]);
                        break;
                }
            }


        }
    };
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        strings = new ArrayList<>();
        for (int i = 1;i<=12;i++){
            strings.add(""+i);
        }
        fragments = new ArrayList<>();
        Guanglist = new ArrayList<>();
        Guanglist.add(new Entry(200,1));
        Guanglist.add(new Entry(300,2));
        Guanglist.add(new Entry(400,3));
        Guanglist.add(new Entry(500,4));
        Guanglist.add(new Entry(600,5));
        Guanglist.add(new Entry(700,6));
        Guanglist.add(new Entry(800,7));
        Guanglist.add(new Entry(900,8));
        Guanglist.add(new Entry(1000,9));
        Guanglist.add(new Entry(1100,10));
        GuanglineChartFragment=new LineChartFragment(getActivity(),"光照强度变化图","光照强度",strings,Guanglist);


        KongList = new ArrayList<>();
        KongList.add(new Entry(10,1));
        KongList.add(new Entry(20,2));
        KongList.add(new Entry(30,3));
        KongList.add(new Entry(40,4));
        KongList.add(new Entry(50,5));
        KongList.add(new Entry(60,6));
        KongList.add(new Entry(70,7));
        KongList.add(new Entry(80,8));
        KongList.add(new Entry(90,9));
        KongList.add(new Entry(100,10));
        KonglineChartFragment = new LineChartFragment(getActivity(),"空气湿度变化图","空气湿度",strings,KongList);



        WenList = new ArrayList<>();
        WenList.add(new Entry(-20,1));
        WenList.add(new Entry(-15,2));
        WenList.add(new Entry(-10,3));
        WenList.add(new Entry(-5,4));
        WenList.add(new Entry(0,5));
        WenList.add(new Entry(5,6));
        WenList.add(new Entry(10,7));
        WenList.add(new Entry(15,8));
        WenList.add(new Entry(20,9));
        WenList.add(new Entry(25,10));
        WenlineChartFragment = new LineChartFragment(getActivity(),"空气温度变化图","空气温度",strings,WenList);
        fragments.add(GuanglineChartFragment);
        fragments.add(KonglineChartFragment);
        fragments.add(WenlineChartFragment);



    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.leco_item_03,null);
        viewPager = (ViewPager) view.findViewById(R.id.leco_item_03_viewpager);
        textView = (TextView) view.findViewById(R.id.leco_item_03_tv);
        fragmentManager = getActivity().getSupportFragmentManager();
        lineChartViewPagerAdapter = new LineChartViewPagerAdapter(fragmentManager,fragments);
        if (lineChartViewPagerAdapter!=null){
            viewPager.setAdapter(lineChartViewPagerAdapter);
        }else {
            Log.d("ViewPage","适配器为空");
        }


        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        new Thread(new Runnable() {
            @Override
            public void run() {
                int code =lineChartViewPagerAdapter.getCount();
                Message message = new Message();
                message.arg1=code;
            }
        }).start();



    }
}
